$(function(){
	$('form').mothinForm();
	
	configOperation_panel('panel',true,function(id,action,name){
		if(name=="添加"){
			$('#organizationAddModal').modal({
				backdrop:'static'
			});
		}else if(name=="删除") {
			__makeChoice('确认删除？',{title:'确定',callback:function(){
				var id=$("#orgId").val();
				$.ajax({
					url:'system/organization!deleteOrganization.action',
					data:{'organization.id':id},
					type: "post"
				}).done(function(data) {
					if(data.ok){
						__showMessage("已成功删除角色",function(){history.back(-1);});
					}else{
						__showMessage(data.message,null,"error");
					}
				});
    		}},{title:'关闭',callback:function(){
    		}});
		} else if(name=="修改"){
			$('#organizationUpdateModal').modal({
				backdrop:'static'
			});
		}else if(name=="设置部门负责人"){
			$(".managerSelector").each(function(){
				var orgId = $("#orgId").val();
				var url="system/organization!selectManager.action?organization.id="+orgId;
				$.ajax({
					url: url,
					dataType:'json',
					success:function(data) {
						if(data&&data.length==0) {
							$(".edit").hide();
							$("#dd").hide();
							__showMessage("该部门暂时没人!","","warning");
						}
						else{
							$('#selectManagerModal').modal({
								backdrop:'static'
							});
							for(i=0;i<data.length;i++){
								$("#sel").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
							}
							$("#sel").bind("change",function(){
								var checkText = $("#sel").find("option:selected").val();
								$("#manager").val(checkText);
							});
						}
					}
				});
			});
			
		}
	});
	
	$("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
        filter: function() {
            return $(this).is(":visible");
        },
        submitSuccess: function($form, event) {
        	console.log($form[0].id);
        	var id=$form[0].id;
        	if(id=="add"){
        		$.ajax({
    				type: 'POST',
    				url: $form.attr('action'),
    				data: $form.serialize(),
    				success: function(data){
    					if(data.ok){
    						$('#roleAddModal').modal('hide');
    						__makeChoice('新机构创建成功',{title:'查看新机构',callback:function(){
    							__navigate('system/organization!view.action',{
            						id:data.value
    							});
    						}},{title:'关闭',callback:function(){
    							location.reload();
    						}},{timer:5000,callback:function(){
    							__navigate('system/organization!view.action',{
            						id:data.value
    							});
    						}});
    					}else{
    						__showMessage('新机构创建失败：'+data.message,null,'error');
    					}
    				}
    			});
        	}else if(id=="update"){
        		$.ajax({
        			type: 'POST',
        			url: $form.attr('action'),
        			data: $form.serialize(),
        			success: function(data){
        				if(data.ok){
        					$('#organizationUpdateModal').modal('hide');
        					__showMessage('机构修改成功',function(){location.reload();},'success');
        				}else{
        					__showMessage('机构修改失败：'+data.message,null,'error');
        				}
        			}
        		});
        	}else if(id=="selectManage"){
        		$.ajax({
        			type: 'POST',
        			url: $form.attr('action'),
        			data: $form.serialize(),
        			success: function(data){
        				if(data.ok){
        					$('#selectManagerModal').modal('hide');
        					__showMessage('负责人设置成功',function(){},'success');
        				}else{
        					__showMessage('负责人设置失败：'+data.message,null,'error');
        				}
        			}
        		});
        	}
     		event.preventDefault();
        }
	});
	
});